﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
using System.IO;
namespace QuanLyDeTaiKHCN.UserControls.Others
{
    public partial class GiaHanDeTai : System.Web.UI.UserControl
    {
        public SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _ps.Decrypt(str, "gold", 128);
                var itemhoidong = _db.tblHopHoiDongs.Where(r => r.MaDeTai.Trim() == _ps.Decrypt(str, "gold", 128));
                if (itemhoidong.Count() == 0)
                {
                    lblMessage.Text = "Đề tài này chưa họp hội đồng xét chọn.";
                    lblMessage.Visible = true;
                    pncontent.Visible = false;
                    IDGiaHan.Value = "";
                }
                else if ((bool)itemhoidong.First().KetQua == false)
                {
                    lblMessage.Text = "Đề tài này không cho phép thực hiện.";
                    lblMessage.Visible = true;
                    pncontent.Visible = false;
                    IDGiaHan.Value = "";
                }
                else
                {
                    BindGrid(madetai);
                    BindGridGiaHan();
                    IDGiaHan.Value = "";
                }
            }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            if (IDGiaHan.Value == "")//them mli
            {
                tblGiaHanDeTai _tb = new tblGiaHanDeTai();
                _tb.MaDeTai = madetai;
                _tb.SoCongVanGiaHan = txtSoCongVanGiaHan.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayGiaHan.Text))
                    _tb.NgayGiaHan = CatalogAccess.ConvertToDate(txtNgayGiaHan.Text);
                if (CatalogAccess.ConvertToDateBool(txtGiaHanToiNgay.Text))
                    _tb.GiaHanToiNgay = CatalogAccess.ConvertToDate(txtGiaHanToiNgay.Text);
                _tb.SoQuyetDinhDongYGiaHan = txtSoQuyetDinhDongYGiaHan.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayDongYGiaHan.Text))
                    _tb.NgayQuyetDinhDongYGiaHan = CatalogAccess.ConvertToDate(txtNgayDongYGiaHan.Text);
                _tb.NoiDungGiaHan = txtNoiDungGiaHan.Text;
                _tb.Lan = CatalogAccess.GetLanGiaHan(madetai);
                try
                {
                    _db.tblGiaHanDeTais.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu.";
                    BindGridGiaHan();
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
            else//cap nhat
            {
                var _tb = _db.tblGiaHanDeTais.Single(r => r.MaDeTai.Trim() == madetai);
                _tb.SoCongVanGiaHan = txtSoCongVanGiaHan.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayGiaHan.Text))
                    _tb.NgayGiaHan = CatalogAccess.ConvertToDate(txtNgayGiaHan.Text);
                if (CatalogAccess.ConvertToDateBool(txtGiaHanToiNgay.Text))
                    _tb.GiaHanToiNgay = CatalogAccess.ConvertToDate(txtGiaHanToiNgay.Text);
                _tb.SoQuyetDinhDongYGiaHan = txtSoQuyetDinhDongYGiaHan.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayDongYGiaHan.Text))
                    _tb.NgayQuyetDinhDongYGiaHan = CatalogAccess.ConvertToDate(txtNgayDongYGiaHan.Text);
                _tb.NoiDungGiaHan = txtNoiDungGiaHan.Text;
                _tb.Lan = CatalogAccess.GetLanGiaHan(madetai);
                try
                {
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu.";
                    BindGridGiaHan();
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            txtSoCongVanGiaHan.Text = "";
            txtNgayGiaHan.Text = "";
            txtGiaHanToiNgay.Text = "";
            txtSoQuyetDinhDongYGiaHan.Text = "";
            txtNgayDongYGiaHan.Text = "";
            txtNoiDungGiaHan.Text = "";
            IDGiaHan.Value = "";
        }

        protected void LinkAddFile_Click(object sender, EventArgs e)
        {
            if (filebienban.HasFile)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _ps.Decrypt(str, "gold", 128);
                DateTime t = DateTime.Now;
                string filename = (t.Day < 10 ? "0" + t.Day.ToString() : t.Day.ToString()) + (t.Month < 10 ? "0" + t.Month.ToString() : t.Month.ToString()) + t.Year.ToString() + t.Hour.ToString() + t.Minute.ToString() + t.Second.ToString() + Path.GetExtension(filebienban.FileName);
                tblFile _tb = new tblFile();
                _tb.MaDeTaiDuAn = madetai;
                _tb.FileThuyetMinh = filename;
                _tb.GhiChuFileThuyetMinh = txtghichufilethuyetminh.Text;
                _tb.NgayNhanThuyetMinh = CatalogAccess.ConvertToDate(txtNgayNhan.Text);
                _tb.FileCuaBuoc = 12;
                try
                {
                    _db.tblFiles.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    BindGrid(madetai);
                    filebienban.SaveAs(Server.MapPath(PageURL.local + "filehoso/" + filename));
                    txtghichufilethuyetminh.Text = "";
                }
                catch { }
            }
        }

        protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            var item = _db.tblFiles.Single(r => r.ID == int.Parse(grid.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblFiles.DeleteOnSubmit(item);
                _db.SubmitChanges();
                try
                {
                    string filename = ((Label)grid.Rows[e.RowIndex].FindControl("lblFileName")).Text;
                    System.IO.File.Delete(Server.MapPath(PageURL.local + "filehoso/" + filename));
                }
                catch { }
                BindGrid(madetai);
            }
            catch { }
        }
        protected void BindGrid(string madetai)
        {
            var list = from p in _db.tblFiles.Where(r => r.MaDeTaiDuAn.Trim() == madetai && r.FileCuaBuoc == 12)
                       select new
                       {
                           ID = p.ID,
                           TrichYeu = p.GhiChuFileThuyetMinh.Trim(),
                           NgayLuu = string.Format("{0:dd/MM/yyyy}", p.NgayNhanThuyetMinh),
                           FileThuyetMinh = p.FileThuyetMinh.Trim(),
                           //sau khi up len host thi xai link1:
                           //Fileonline =domain_name + "\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //test xaif link2
                           //Fileonline = "localhost\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //demo link3:
                           Fileonline = "http://thongkekhcnbd.khcnbinhduong.gov.vn/userfiles/vanban/9475707965_493_296.pdf"

                       };
            grid.DataSource = list.ToList();
            grid.DataBind();
        }

        protected void BindGridGiaHan()
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            var list = from p in _db.tblGiaHanDeTais
                       where p.MaDeTai.Trim() == madetai
                       select new
                       {
                           ID=p.ID,
                           SoCongVanGiaHan=p.SoCongVanGiaHan.Trim(),
                           NgayGiaHan=string.Format("{0:dd/MM/yyyy}",p.NgayGiaHan),
                           GiaHanToiNgay = string.Format("{0:dd/MM/yyyy}", p.GiaHanToiNgay),
                           SoQuyetDinhGiaHan=p.SoQuyetDinhDongYGiaHan.Trim(),
                           NgayQuyetDinhGiaHan= string.Format("{0:dd/MM/yyyy}", p.NgayQuyetDinhDongYGiaHan),
                           Lan=p.Lan,
                           NoiDung=p.NoiDungGiaHan.Trim()
                       };
            gridgiahan.DataSource = list.ToList();
            gridgiahan.DataBind();
        }

        protected void gridgiahan_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int sdfsdfsd = int.Parse(gridgiahan.DataKeys[e.RowIndex].Value.ToString());
            var item = _db.tblGiaHanDeTais.Single(r => r.ID == int.Parse(gridgiahan.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblGiaHanDeTais.DeleteOnSubmit(item);
                _db.SubmitChanges();
                BindGridGiaHan();
                txtSoCongVanGiaHan.Text = "";
                txtNgayGiaHan.Text = "";
                txtGiaHanToiNgay.Text = "";
                txtSoQuyetDinhDongYGiaHan.Text = "";
                txtNgayDongYGiaHan.Text = "";
                txtNoiDungGiaHan.Text = "";
                IDGiaHan.Value = "";
            }
            catch { }
        }

        protected void gridgiahan_RowEditing(object sender, GridViewEditEventArgs e)
        {
            txtSoCongVanGiaHan.Text = ((Label)gridgiahan.Rows[e.NewEditIndex].FindControl("lbl1")).Text;
            txtNgayGiaHan.Text = ((Label)gridgiahan.Rows[e.NewEditIndex].FindControl("lbl2")).Text;
            txtGiaHanToiNgay.Text = ((Label)gridgiahan.Rows[e.NewEditIndex].FindControl("lbl3")).Text;
            txtSoQuyetDinhDongYGiaHan.Text = ((Label)gridgiahan.Rows[e.NewEditIndex].FindControl("lbl4")).Text;
            txtNgayDongYGiaHan.Text = ((Label)gridgiahan.Rows[e.NewEditIndex].FindControl("lbl5")).Text;
            txtNoiDungGiaHan.Text = ((Label)gridgiahan.Rows[e.NewEditIndex].FindControl("lblNoiDung")).Text;
            IDGiaHan.Value = ((Label)gridgiahan.Rows[e.NewEditIndex].FindControl("lblID")).Text;
        }
    }
}